package srzy.module

import srzy.common.Logging
import srzy.diameter._
import srzy.rb.RBMsg
import srzy.rb.RBMsgParserException

object RBMsgComp extends AbsCaseDeal with Logging {

  def deal = {
    val realReceFromSMMsgs = getCaseInfo.realReceFromSMMsg
    val stdReceFromSMMsgs = getCaseInfo.stdReceFromSMMsg

    if (realReceFromSMMsgs.size != stdReceFromSMMsgs.size) {
      logger.debug("stdReceFromSMMsgs size " +
        stdReceFromSMMsgs.size + " not equal to realReceFromSMMsgs size "
        + realReceFromSMMsgs.size + " !")
      getCaseInfo.errLog += ("stdReceFromSMMsgs size " +
        stdReceFromSMMsgs.size + " not equal to realReceFromSMMsgs size " +
        realReceFromSMMsgs.size + " !" + "\n")
    } else {
      if (realReceFromSMMsgs.size + stdReceFromSMMsgs.size == 0) {
        getCaseInfo.receFromSMMsgCompResult = true
      } else {

        val res = (0 until realReceFromSMMsgs.size).toList.map(i => {
          val realMsg = realReceFromSMMsgs.apply(i)
          val stdMsg = stdReceFromSMMsgs.apply(i)

          var res: Boolean = true
          var compLog: String = ""
          logger.debug("stdMsg :" + stdMsg)
          logger.debug("realMsg:" + realMsg)
          val r = RBMsg.compare2(stdMsg, realMsg)
          res = r._1
          if (res == false) {
            val txt = "ReceFromSMMsg[" + (i + 1) + "]\n" + r._2 + "\n"
            getCaseInfo.receFromSMMsgLog += txt
          }
          r
        })

        if (res.exists(_._1 == false)) {
          logger.debug("ReceFromSMMsg has differents\n" +
            getCaseInfo.receFromSMMsgLog.split("\n").toList.map("  " + _).mkString("\n"))
          getCaseInfo.errLog += "ReceFromSMMsg has differents\n" +
            getCaseInfo.receFromSMMsgLog.split("\n").toList.map("  " + _).mkString("\n") + "\n"
        } else {
          getCaseInfo.receFromSMMsgCompResult = true
        }
      }
    }

    val realSendToSMMsgs = getCaseInfo.realSendToSMMsg
    val stdSendToSMMsgs = getCaseInfo.stdSendToSMMsg

    if (realSendToSMMsgs.size != stdSendToSMMsgs.size) {
      logger.debug("stdSendToSMMsgs size " + stdSendToSMMsgs.size +
        " not equal to realSendToSMMsgs size "
        + realSendToSMMsgs.size + " !")
      getCaseInfo.errLog += ("stdSendToSMMsgs size " + stdSendToSMMsgs.size +
        " not equal to realSendToSMMsgs size "
        + realSendToSMMsgs.size + " !" + "\n")
    } else {
      if (realSendToSMMsgs.size + stdSendToSMMsgs.size == 0) {
        getCaseInfo.sendToSMMsgCompResult = true
      } else {
        val res = (0 until realSendToSMMsgs.size).toList.map(i => {
          val realMsg = realSendToSMMsgs.apply(i)
          val stdMsg = stdSendToSMMsgs.apply(i)

          var res: Boolean = true
          var compLog: String = ""
          logger.debug("stdMsg :" + stdMsg)
          logger.debug("realMsg:" + realMsg)
          val r = RBMsg.compare2(stdMsg, realMsg)
          res = r._1
          if (res == false) {
            val txt = "SendToSMMsg[" + (i + 1) + "]\n" + r._2 + "\n"
            getCaseInfo.sendToSMMsgLog += txt
          }
          r
        })

        if (res.exists(_._1 == false)) {
          logger.debug("sendToSMMsg has differents\n" +
            getCaseInfo.sendToSMMsgLog.split("\n").toList.map("  " + _).mkString("\n"))
          getCaseInfo.errLog += "sendToSMMsg has differents\n" +
            getCaseInfo.sendToSMMsgLog.split("\n").toList.map("  " + _).mkString("\n") + "\n"
        } else {
          getCaseInfo.sendToSMMsgCompResult = true
        }

        //        var flag = true
        //        val iter = (0 until realSendToSMMsgs.size).toList.iterator
        //
        //        while (iter.hasNext && flag == true) {
        //          val i = iter.next
        //          val realMsg = realSendToSMMsgs.apply(i)
        //          val stdMsg = stdSendToSMMsgs.apply(i)
        //
        //          var res: Boolean = true
        //          var compLog: String = ""
        //          //        try {
        //          logger.debug("stdMsg :" + stdMsg)
        //          logger.debug("realMsg:" + realMsg)
        //          val r = RBMsg.compare2(stdMsg, realMsg)
        //          res = r._1
        //          getCaseInfo.sendToSMMsgLog = r._2
        //          if (res == true) {
        //            getCaseInfo.sendToSMMsgCompResult = true
        //          }
        //        }
      }
    }

  }

  //  def fail = {
  //    getCaseInfo.rbMsgCompResult = false
  //    getCaseInfo.caseResult = false
  //  }

  //  def success = {
  //    getCaseInfo.receFromMsgCompResult = true
  //    //    getCaseInfo.caseResult = true
  //  }
}